/**
 * 
 */
package com.block.common.security.service.impl;


import com.block.common.security.service.AppEncryptMannerService;
import com.block.common.security.util.AESUtil;
import com.block.db.entity.AppEncrypt;

import java.io.UnsupportedEncodingException;


/**
 * 
 * AES加密方式
 */
public class AESEncryptManner implements AppEncryptMannerService {
	
	//加密
	@Override
	public String encode(AppEncrypt appEncrypt, String tranData, String cipherKey) {
		return AESUtil.parseByte2HexStr(AESUtil.encrypt(tranData, cipherKey));
	}

	//解密
	@Override
	public String decode(AppEncrypt appEncrypt, String tranData, String cipherKey) {
		byte[] decryptFrom = AESUtil.parseHexStr2Byte(tranData);  
		byte[] decryptResult = AESUtil.decrypt(decryptFrom, cipherKey);
		//解密后数据【AES】
		try {
			return new String (decryptResult, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void x()
	{
		Long start = System.currentTimeMillis();
		String c = new AESEncryptManner().encode(null, "question=1ujasoda90123u&index=0", "asdasdzxc981243JOIASasdj1239ASD");
		Long end = System.currentTimeMillis();
		System.out.println(end - start);
	}
	
	public static void main(String[] args) {
		long start = 0;
		long end = 0;
		for (int i = 0; i < 10; i++) {
			start = System.currentTimeMillis();
			String c = new AESEncryptManner().encode(null, "question=1ujasoda90123u&index=0", "asdasdzxc981243JOIASasdj1239ASD");
			end = System.currentTimeMillis();
			System.out.println(end - start);
		}
//		System.out.println(c);
//		c = new AESEncryptManner().decode(null, c, "asdasdzxc981243JOIASasdj1239ASD");
//		System.out.println(c);
	}
}
